package openapi

import (
	"github.com/gin-gonic/gin"
	"github.com/winjeg/go-commons/openapi"
	"go-admin/common/global"
	"go-admin/common/log"
	"net/http"
)

/**
* @Description:
* @Author: yuweizhu
* @Date: 2021/2/27 5:06 下午
**/
func HandleOpenApiAuth() gin.HandlerFunc {
	return func(c *gin.Context) {
		defer func() {
			if err := recover(); err != nil {
				log.Warn("auth fail ", err)
			}
		}()
		req := c.Request
		r, err := openapi.CheckValid(req,
			// default implementation is via sql, to fetch the secrect
			openapi.SqlSecretKeeper{
				Db:        global.SqlDB,
				TableName: "t_openapi_app",        // the name of table where you store all your app  keys and  secretcs
				KeyCol:    "app_key",    // the column name of the app keys
				SecretCol: "app_secret", // the column name of the app secrets
			})
		if !r || err != nil {
			c.Status(http.StatusForbidden)
			c.Abort()
			return
		}
		c.Next()
	}
}

